var app = angular.module("NTD",[]);

//app.constant('path',window.localStorage['path']);
app.constant('path','http://ntd.com');

app.run(function($rootScope) {
    var is_login = window.localStorage['client_is_login'] == 'true';
    var is_login_page = window.location.href.indexOf('login.html') !== -1;
    if(!is_login_page && !is_login) {
        window.location.href = './login.html';
    }

});

app.config(function($httpProvider) {

});

app.factory('apiService',['$http','path',function($http,path){
    return {
        get:function(url,params) {
            return $http({
                method:'GET',
                url:path + url,
                timeout:10000,
                data:params
            });
        }
    };
}]);

/*======================= Controller ==============================*/
app.controller("loginController",['$scope','apiService',function($scope) {
    $scope.signIn = function() {
        var user = $scope.user || {};
        user.verify = false;
        if(!user.SerialNumber) {
            $(".tip").addClass('login-err').html("请输入登录账号!").show();
            user.verify = false;
        } else if(!user.Password) {
            $(".tip").addClass('login-err').html("请输入登录密码!").show();
            user.verify = false;
        } else if(user.SerialNumber !== 'admin' || user.Password !== 'Njs') {
            $(".tip").addClass('login-err').html("账号或密码不正确!").show();
            user.verify = false;
        } else {
            user.verify = true;
        }
        if(user.verify !== false) {
            window.localStorage['client_is_login'] = true;
            $(".tip").removeClass("login-err").html("登录成功,请稍后...").show();
            window.location.href = './index.html';
        }
    };

    $scope.visitorsIn = function() {
        $(".tip").removeClass("login-err").html("即将以游客进入程序,请稍后...").show();
    };

    $scope.register = function(){
        $(".tip").removeClass("login-err").html("暂不支持注册!..").show();
    };
}]);

app.controller("IndexController" , ['$scope','$interval','apiService',function($scope,$interval,apiService) {

    $scope.clock = new Date();
    $interval(function(){
        $scope.clock = new Date();
    },1000);

    apiService.get('/task/list.json',{}).success(function(data){
        $scope.list = data;
    });

}]);


/*====================== Directive ===============================*/
app.directive("loginForm" , function(){
    //效果来源 : http://thecodeplayer.com/walkthrough/animating-float-labels-jquery-css3
    return {
        restrict:"AE",
        link:function(scope,ele,attr) {
            var d;
            $(ele).find("label").each(function(){
                var sop = '<span class="ch">'; //span opening
                var scl = '</span>'; //span closing
                $(this).html(sop + $(this).html().split("").join(scl+sop) + scl);
                $(".ch:contains(' ')").html("&nbsp;");
            });
            $(ele).find("input").focus(function(){
                var tm = $(this).outerHeight()/2 *-1 + "px";
                $(this).next().addClass("focussed").children().stop(true).each(function(i){
                    d = i*50;//delay
                    $(this).delay(d).animate({top: tm}, 200, 'easeOutBack');
                });
            }).blur(function(){
                if($(this).val() === "") {
                    $(this).next().removeClass("focussed").children().stop(true).each(function(i){
                        d = i*50;
                        $(this).delay(d).animate({top: 0}, 500, 'easeInOutBack');
                    });
                }
            });
        }
    };
});

app.directive("loginOut" , function(){
    return {
        restrict:"AE",
        link:function(scope,ele,attr) {
            $(ele).click(function(){
                window.localStorage['client_is_login'] = false;
                window.location.href = './login.html';
            });
        }
    }
});

app.directive("iCheck",function(){
    return {
        restrict:"AE",
        link:function(scope,ele,attr) {
            $(ele).iCheck({
                checkboxClass: 'icheckbox_square-blue',
                radioClass: 'iradio_square-blue',
                increaseArea: '20%'
            });


        }
    };
});